//
//  CSSStatusPictureView.swift
//  weibo28
//
//  Created by chushushan on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


// 每个item之间的间距
let itemMargin: CGFloat = 5
//计算每个图片的宽度
let itemWidth: CGFloat = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin) / 3

//item重用标识
private let CSSStatusPictureViewCellIdentifier = "CSSStatusPictureViewCellIdentifier"


class CSSStatusPictureView: UICollectionView, UICollectionViewDataSource {

    //绑定数据
    var picUrls: [CSSStatusPictureInfo]? {
    
        didSet {
        
//        messageLabel.text = "\(picUrls?.count ?? 0)"
            
            let currentSize = calcSize(picUrls?.count ?? 0)
            //根据图片数量更新size
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(currentSize)
            }
           //刷新数据
            reloadData()
        }
    
    }
    
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 20)
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        //设置itemsize
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //设置垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        
//注册cell
        registerClass(CSSStatusPictureViewCell.self, forCellWithReuseIdentifier: CSSStatusPictureViewCellIdentifier)

        
//增加控件设置约束
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
//    backgroundColor = RandomColor()
        
//添加子控件
        addSubview(messageLabel)
        

        
//设置约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //设置数据源代理
        dataSource = self
    
    }
    
    
//根据图片数量生成CGSize
    private func calcSize(count: Int) -> CGSize {
    
        //列数
        let cols = count > 3 ? 3 : count
        //行数
        let rows = (count - 1) / 3 + 1
        //当前视图的宽度
        let currentWidth = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //当前视图的高度
        let currentHeight = CGFloat(rows) * itemWidth + CGFloat(rows - 1) * itemMargin
        return CGSize(width: currentWidth, height: currentHeight)
    
    }
    
    
    
// MARK: - 数据源代理方法
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CSSStatusPictureViewCellIdentifier, forIndexPath: indexPath) as! CSSStatusPictureViewCell
        
//        cell.backgroundColor = RandomColor()
        
        //给cell传一个模型
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
        
    }
    

}


// MARK: - 自定义cell

class CSSStatusPictureViewCell: UICollectionViewCell {
    
    
    //设置模型
    var picInfo: CSSStatusPictureInfo? {
    
        didSet {
            
            guard let urlStr = picInfo?.thumbnail_pic else {
            return
            }
        
        imageView.sd_setImageWithURL(NSURL(string: urlStr), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            //判断是否是gif
            gifImageView.hidden = !urlStr.hasSuffix(".gif")
        
        }
    }
    
    private lazy var imageView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //设置图片等比填充
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //多余部分截取掉
        view.clipsToBounds = true
        
        return view
    }()
    
    //gif图片
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI() {
    
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(CGSizeZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.right.bottom.equalTo(contentView)
        }
        
    }
    
}




















